package com.ff.neuropp.service;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ff.neuropp.domain.IosUser;
import com.ff.neuropp.repository.IosUserRepository;
import com.ff.neuropp.util.MaiUtil;
import com.ff.neuropp.util.Md5Util;
import com.ff.neuropp.util.ResultTool;
import com.ff.neuropp.util.RyUtils;
import com.ff.neuropp.util.SnowIdUtil;

/**
 *  ios用户服务
 */
@Service
public class IosUserService {

	@Autowired
    private IosUserRepository iosUserRepository;

	@Autowired
	private MaiUtil maiUtil ;

	@Autowired
	private RyUtils ryUtils;


	public Map<String,Object> addUser(IosUser param ) {


		IosUser user =  iosUserRepository.findByEmail(param.getEmail());
		if(user == null){
			//未查到用户信息
			return ResultTool.genFailResult(ResultTool.ErrorCode_1,"根据邮箱未查到用户信息");
		}
		//userid 不为空 说明已注册
		//TODO 油箱加验证码登录 ？
		if(StringUtils.isNotEmpty(user.getUserId())){
			//未查到用户信息
			return ResultTool.genFailResult(ResultTool.ErrorCode_1,"该邮箱已经注册");
		}

		//校验验证码
		if(user.getCode().equals(param.getCode())){
			//生成 userid
			//TODO 其他信息
			String userid = SnowIdUtil.userId();
			user.setUserId(userid);
			//添加融云token
			String token = ryUtils.getToken(userid,param.getUserName(),param.getHeadUrl());
			user.setRyToken(token);
			user.setEmail(param.getEmail());
			user.setUserName(param.getUserName());//谷歌用户名称
			user.setPassword(Md5Util.Encrypt(param.getPassword()));
			IosUser data = iosUserRepository.save(user);
			return ResultTool.genSuccessResult(data);
		}else{
			return ResultTool.genFailResult(ResultTool.ErrorCode_2,"验证码有误");
		}

	}
	
	public Map<String,Object> sendCode (String email){

		//String code =  maiUtil.gmailSender(email);
		String code =  "666888";
		if(StringUtils.isEmpty(code)){
			return ResultTool.genFailResult(100002, "发送失败");
		}else{
			//用户表添加信息
			IosUser user = new IosUser();
			user.setCode(code);
			user.setEmail(email);
			user.setLoginType("2");//gmail
			//TODO 其他默认信息
			iosUserRepository.save(user);
		}
		return ResultTool.genSuccessResult("发送成功");
	}




	public Map<String,Object> login (IosUser param){
		//Gmail登录验证
        if("2".equals(param.getLoginType())) {
    		IosUser userinfo =  iosUserRepository.findByEmail(param.getEmail());
    		if(userinfo == null){
    			//未查到用户信息
    			return ResultTool.genFailResult(ResultTool.ErrorCode_1,"用户未注册");
    		}
        	String password = Md5Util.Encrypt(param.getPassword());
    		param.setPassword(password);
    		IosUser user = iosUserRepository.findByEmailAndPassword(param.getEmail(),param.getPassword());
    		if(user !=null ){
    			return ResultTool.genSuccessResult(user);
    		}
    		 return ResultTool.genFailResult(100002, "用户名或密码错误");	
        }else {
        	//facebook授权登录
        	IosUser user =  iosUserRepository.findByUserId(param.getUserId());
        	if(user == null){
        		IosUser user2 =new IosUser();
        		//facebook自身 userid
    			user2.setUserId(param.getUserId());
    			user2.setUserName(param.getUserName());
    			user2.setEmail(param.getEmail());
    			user2.setPassword(null);
    			user2.setLoginType("1");//facebook
				//添加融云token
				String token = ryUtils.getToken(param.getUserId(),param.getUserName(),param.getHeadUrl());
				user2.setRyToken(token);
    			IosUser data = iosUserRepository.save(user2);
        		//登录成功
        		return ResultTool.genSuccessResult(data);
        	}else {
        		//登录成功
        		return ResultTool.genSuccessResult(user);
        	}
        }
	}

	public IosUser findByUserId(String userId) {
		IosUser user =  iosUserRepository.findByUserId(userId);
		return user;
	}

	/**
	 * 修改用戶信息
	 * @param iosUser
	 * @return
	 */
	public IosUser saveUser(IosUser iosUser) {
		return iosUserRepository.save(iosUser);
		
	}

}
